<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>输入两个数字，比较大小</title>
  </head>
  <body>
    <input type="text" class="f-text" /><strong>vs</strong
    ><input type="text" class="f-text" /><input
      type="button"
      value="最大的数是 &raquo;"
      class="f-btn"
    /><span></span>
  </body>
  <script>
    var aInput = document.getElementsByTagName("input");
    var aSpan = document.getElementsByTagName("span")[0];
    var i = 0;
    for (i = 0; i < aInput.length - 1; i++) {
      aInput[i].onkeyup = function () {
        this.value = this.value.replace(/[^\d]/, "");
      };
    }
    aInput[2].onclick = function () {
      aInput[0].value == "" || aInput[1].value == ""
        ? alert("请输入数字！")
        : (aSpan.innerHTML = Math.max(aInput[0].value, aInput[1].value));
    };
  </script>
</html>
